package net.linestone.myapplication;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class MyActivity extends Activity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        if (Build.VERSION.SDK_INT >= 11) {
            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
        }


        Log.i("ff", "开始");
        try {
            Log.i("ff", "try");
            requestByHttpGet();
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("ff", "异常");
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    // HttpGet方式请求  
    public static void requestByHttpGet() throws Exception {
        String path = "http://www.163.com";
        Log.i("ff", "1");
        // 新建HttpGet对象  
        HttpGet httpGet = new HttpGet(path);
        Log.i("ff", "2");
        // 获取HttpClient对象  
        HttpClient httpClient = new DefaultHttpClient();
        Log.i("ff", "3");
        // 获取HttpResponse实例  
        HttpResponse httpResp = httpClient.execute(httpGet);
        Log.i("ff", "4");
        // 判断是够请求成功  
        if (httpResp.getStatusLine().getStatusCode() == 200) {
            // 获取返回的数据  
            String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
            Log.i("ff", "HttpGet方式请求成功，返回数据如下：");
            Log.i("ff", result);
        } else {
            Log.i("ff", "HttpGet方式请求失败");
        }
    }
    private static byte[] readStream(InputStream inputStream) throws Exception {
        byte[] buffer = new byte[1024];
        int len = -1;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((len = inputStream.read(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }
        byte[] data = baos.toByteArray();
        inputStream.close();
        baos.close();
        return data;
    }
}